package com.zhkj.shixi.tvplayer.util;

import android.content.Context;
import android.os.Environment;
import android.os.storage.StorageManager;

import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("unchecked")
public class USBUtil {
    public static List getUSBPaths(Context con){//反射获取路径
        String[] paths = null;
        List data = new ArrayList();    // include sd and usb devices
        StorageManager storageManager = (StorageManager) con.getSystemService(Context.STORAGE_SERVICE);
        try {

            paths = (String[]) StorageManager.class.getMethod("getVolumePaths", null).invoke(storageManager, null);
            for (String path : paths){
                String state = (String) StorageManager.class.getMethod("getVolumeState",String.class).invoke(storageManager, path);
                if (state.equals(Environment.MEDIA_MOUNTED) && !path.contains("emulated")) {
                    data.add(path);
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return data;
    }
}
